package com.example.demo.mapper;

import com.example.demo.pojo.Jilue;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface JilueMapper {

    // 查询所有记录或根据查询条件模糊查询
    @Select("<script>" +
            "SELECT * FROM jilue " +
            "<if test='query != null and query != \"\"'>" +
            "WHERE name LIKE CONCAT('%', #{query}, '%') OR id LIKE CONCAT('%', #{query}, '%')" +
            "</if>" +
            "</script>")
    List<Jilue> findAll(@Param("query") String query);

    // 添加新记录
    @Insert("INSERT INTO jilue (id, name, role, department, class_name, info, status) " +
            "VALUES (#{id},#{name}, #{role}, #{department}, #{class_name}, #{info}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 自动生成主键
    int insertJilue(Jilue jilue);

    // 更新记录
    @Update("UPDATE jilue SET " +
            "name = #{name}, " +
            "role = #{role}, " +
            "department = #{department}, " +
            "class_name = #{class_name}, " +
            "info = #{info}, " +
            "status = #{status} " +
            "WHERE id = #{id}")
    int updateJilue(Jilue jilue);

    // 根据 ID 删除记录
    @Delete("DELETE FROM jilue WHERE id = #{id}")
    int deleteJilue(@Param("id") Long id);

    @Select("SELECT * FROM jilue WHERE id = #{id}")
    Jilue findById(Long id);
}
